package cn.com.Servlet.UserServlet;

import cn.com.controler.User.UserDAO;
import cn.com.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "DataServlet",value = "/DataDML")
public class DataServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doPost(request,response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("html/text;charset=utf-8");
        String datatype = request.getParameter("datatype");
        if(datatype.equals("cx")){    //修改时查询我的资料
            String idd = request.getParameter("id");
            Integer id = Integer.valueOf(idd);
            UserDAO userDAO = new UserDAO();
            List idselect = userDAO.idselect(id);
            User use = (User) idselect.get(0);
            request.setAttribute("user",use);
            request.getRequestDispatcher("/WebContent/HTML/UserPage/user_user/AlterData.jsp").forward(request,response);
        }else if(datatype.equals("xg")){      //修改我的信息
            //获取数据
            HttpSession session = request.getSession();
            User user = (User)session.getAttribute("user");
            Integer userId = user.getUserId();
            String username = request.getParameter("username");
            String birtime = request.getParameter("birtime");
            String tel = request.getParameter("tel");
            String email = request.getParameter("email");
            //创建DAO对象
            UserDAO userDAO = new UserDAO();
            //修改数据
            int updateuser = userDAO.updateuser(username, birtime, tel, email, userId);
            if(updateuser ==1){
                response.getWriter().write("修改成功！");
            }else{
                response.getWriter().write("抱歉，修改失败！");
            }


        }else if(datatype.equals("cxmm")){    //修改密码时查询
            String idd = request.getParameter("id");
            Integer id = Integer.valueOf(idd);
            UserDAO userDAO = new UserDAO();
            List idselect = userDAO.idselect(id);
            User usemm = (User) idselect.get(0);
            request.setAttribute("usermm",usemm);
            request.getRequestDispatcher("/WebContent/HTML/UserPage/user_user/AlterPwd.jsp").forward(request,response);
        }else if(datatype.equals("xgmm")){    //修改
            String password = request.getParameter("password");
            String idd = request.getParameter("id");
            Integer id = Integer.valueOf(idd);
            UserDAO userDAO = new UserDAO();
            int updatepass = userDAO.updatepass(password, id);
            if(updatepass == 1){
                response.setCharacterEncoding("utf-8");
                response.getWriter().print(updatepass);
            }else{
                response.getWriter().print(0);
            }
        }


    }
}
